﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class UNZIPTargetNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            UNZIPTargetNParam? param = item.ControlParam as UNZIPTargetNParam;
            string zipFilePath = param.ZipFilePath;
            string unzipDir = param.UnzipDir;
            string outVarName = param.OutVarName;
            string zipPassword = param.UnzipPassword;
            bool createDir = param.CreateDir;
            this.GetStringVar(ref zipFilePath, globleData);
            this.GetStringVar(ref unzipDir, globleData);
            FileInfo zipFile = new FileInfo(zipFilePath);
            if(createDir) { 
                string newDirName = zipFile.Name.Replace(zipFile.Extension, "");
                unzipDir = Directory.CreateDirectory(unzipDir + Path.DirectorySeparatorChar + newDirName).FullName;
            }
            switch(zipFile.Extension) {
                case ".zip":
                    CompressUtil.ZipDeCompress(zipFilePath, unzipDir, zipPassword);
                    break;
                case ".rar":
                    CompressUtil.ZipDeCompress(zipFilePath, unzipDir, zipPassword);
                    break;
                default:
                    CompressUtil.ZipDeCompress(zipFilePath, unzipDir, zipPassword);
                    break;
            }
            this.saveVar(globleData, outVarName, new StringModel(outVarName, unzipDir), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
